﻿// *****************************************************************************
// **                   Ladenius MediaCenter TVSeries                         **
// ** ----------------------------------------------------------------------- **
// ** (c) 2008 by Jurjen Ladenius                                             **
// *****************************************************************************

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ladenius.Series.Path.Business;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
#endregion

namespace Ladenius.Series.Path.Business {
    /// <summary>
    /// a parsing rule
    /// </summary>
    internal class ParsingRule {

        #region Fields
        private string _text;
        private Regex _regularExpression;
        private List<string> _keys;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ParsingRule"/> class.
        /// </summary>
        internal ParsingRule() {
            _keys = new List<string>();
            _regularExpression = null;
            _text = string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ParsingRule"/> class.
        /// </summary>
        /// <param name="parsingRule">The parsing rule.</param>
        internal ParsingRule(string parsingRule)
            : this() {
            Text = parsingRule;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        internal string Text {
            get { return _text; }
            set {
                _text = value;
                SetRegularExpression();
                SetParsingKeys();
            }
        }

        /// <summary>
        /// Gets or sets the regular expression.
        /// </summary>
        /// <value>The regular expression.</value>
        internal Regex RegularExpression {
            get { return _regularExpression; } 
        }

        /// <summary>
        /// Gets the parsing keys.
        /// </summary>
        /// <value>The parsing keys.</value>
        internal ReadOnlyCollection<string> Keys {
            get { return _keys.AsReadOnly(); }
        }
        #endregion


        #region Private methods
        /// <summary>
        /// Sets the regular expression.
        /// </summary>
        private void SetRegularExpression() {
            string regularExpression = _text.ToLower();
            regularExpression = regularExpression.Replace(@"\", @"\\");
            regularExpression = regularExpression.Replace(@"<series>", @"([\'|\""|\.|\(|\)|\[|\]|\^|\!|\;|\`|\@|\&|\-|\=|\%\w|\s|\d]+)");
            regularExpression = regularExpression.Replace(@"<episodename>", @"([\'|\""|\.|\(|\)|\[|\]|\^|\!|\;|\`|\@|\&|\-|\=|\%\w|\s|\d]+)");
            regularExpression = regularExpression.Replace(@"<episodenumber>", @"(\d+)");
            regularExpression = regularExpression.Replace(@"<seasonnumber>", @"(\d+)");
            regularExpression = regularExpression.Replace(@"<extension>", @"([\'|\""|\.|\(|\)|\[|\]|\^|\!|\;|\`|\@|\&|\-|\=|\%\w|\s|\d]+)");
            _regularExpression = new Regex(regularExpression, RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// Sets the parsing keys.
        /// </summary>
        private void SetParsingKeys() {
            _keys.Clear();
            Regex regex = new Regex(@"<([\w\d\s]+)\>");
            foreach (Match match in regex.Matches(_text))
                _keys.Add(match.Value.ToLower().Replace("<", "").Replace(">", ""));
            _keys.Remove("extension");
        }
        #endregion


    }
}
